// pages/menu/menu.js


// var amapFile = require("../../libs/amap-wx.js")
var bmap = require('../../libs/bmap-wx.js'); 
var app = getApp();
var wxMarkerData = []; 
var sugDataList=[];
var BMap;
let city;
let typename_now="美食";
let inex_now="0";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menu:['美食','酒店','生活服务'],
    markers: [],
    latitude: '',
    longitude: '',
    textData: {},
    placeData: {} ,
    page_num:1,
    pagesize:10,
    region:'',
    city:'',
    selected: 0,
    selected_name:"美食",
    listFlag:false,
    // swiperTitle: [{
    //   text: "点菜",
    //   id: 1
    // },{
    //   text: "评价",
    //   id: 2
    // },{
    //   text: "商家",
    //   id: 3
    // }],

    // 页面假数据



    // menu:[],
    // currentPage: 0,
    // selected: 0,
    // howMuch: 12,
    // cost:0,
    // pullBar: false
  },

  makertap: function (e) {
    var id = e.markerId;
    var that = this;
   
    that.showMarkerInfo(markersData, id);
    that.changeMarkerColor(markersData, id);
  },
  onLoad: function () {
    var that = this;
    var page=that.data.page_num;
    var pagesize = that.data.pagesize;
    console.log(that);
    // var myAmapFun = new amapFile.AMapWX({ key: '40e035b7ea9ccd40a910c8952b2f2345' });
    // myAmapFun.getPoiAround({
    //   success: function (data) {
    //     //成功回调
    //     console.log(data);

    //   },
    //   fail: function (info) {
    //     //失败回调
    //     console.log(info)
    //   }
    // })
    // 新建百度地图对象 
   BMap = new bmap.BMapWX({
      ak: 'bzQsfmU86UsaqMOeZzXFrjbkVAUKcVYW'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      // var sugData = ''; 

      // for (var i = 0; i < data.result.length; i++) {
      //   sugData = sugData + data.result[i].name + '\n';
      // }
      // that.setData({
      //   sugData: sugData,
       
      // }); 
        
      }
    // 发起POI检索请求 
    BMap.search({
      "query": '美食',
      page_num:1,
      page_size: pagesize,
      success: function(data){
       that.setData({
          wxMarkerData : data.wxMarkerData,
          markers: wxMarkerData,
          // latitude: wxMarkerData[0].latitude,
          // longitude: wxMarkerData[0].longitude
        })
      },
      // 此处需要在相应路径放置图片文件 
      // iconPath: '../../img/marker_red.png',
      // 此处需要在相应路径放置图片文件 
      // iconTapPath: '../../img/marker_red.png'
    }); 
  },

  onPullDownRefresh: function (e) {
    console.log("下拉刷新");
  
    var that = this;
    console.log(that);
    var typename =that.data.selected_name;
    var index_now = that.data.selected;
    var pagesize = that.data.pagesize;
    console.log(typename);
    console.log(index_now);
    // var page = that.data.page_num;
    // this.getMusicInfo('正在刷新数据')
    //展示部分
    //提示数据再刷新
    wx.showToast({
      title: '正在刷新数据',
      icon:"loading",
      duration:500
    })
   //停止刷新
    setTimeout(function () { 
      wx.stopPullDownRefresh()
      },500)

      //改变当前页面
    var page = that.data.page_num - 1;
    if(page<1){
      page=1
    }
    that.setData({
      page_num:page
    })
    console.log(page);
      //请求百度api
    BMap.search({
      "query": typename,
      location: '',
      page_size: pagesize,
      page_num:page,
      fail: function (data) {
        console.log(data);
      },
      success: function (data) {
        that.setData({
          wxMarkerData: data.wxMarkerData,
          markers: wxMarkerData,
          selected: index_now

        })
      },

    })




  },

  onReachBottom: function () {
    console.log("上啦加载")
    var that = this;
    var page = that.data.page_num+1;
    var typename=that.data.selected_name;
    var index_now=that.data.selected;
    var pagesize=that.data.pagesize;
    that.setData({
      page_num:page,
      
    })
   
    // 发起POI检索请求 
    BMap.search({
      "query": typename,
      page_size: pagesize,
      page_num:page,
      success: function (data) {
        console.log(data.wxMarkerData.length);
        that.setData({
          wxMarkerData: data.wxMarkerData,
          markers: wxMarkerData,
          // latitude: wxMarkerData[0].latitude,
          // longitude: wxMarkerData[0].longitude
        })
      },error:function(){
        wx.showToast({
          title: '没有更多数据',
        })
        
      }
      
    }); 

wx.showToast({
  title: '正在加载中',
  icon: 'loading',
  duration:500
})
     
  },

    //点击左侧导航栏搜索
  searchMap: function (e){
      console.log(e);
      var typename = e.currentTarget.dataset.name;
      var pagesize = 15;
      // console.log(typeName);
     var that=this;
     that.setData({
       selected_name:typename,
       selected: e.currentTarget.dataset.index,
       page_num:1
     })
      BMap.search({
        "query": typename,
        location:'',
        page_size: pagesize,
        fail: function(data){
          console.log(data);
        },
        success: function (data) {
          that.setData({
            wxMarkerData: data.wxMarkerData,
            markers: wxMarkerData,
            selected: e.currentTarget.dataset.index
           
          })
        },
        
      })

    
    },

  //页面滑动到底部
     bindDownLoad:function() {
          // loadMore(that);
           console.log("*********滚动到底部************");
   
  },
   
  /*********以上onload**********/
  // 绑定input输入 
  bindKeyInput: function (e) {
    console.log("zhengzaishuru");
    var that = this;
    var region;
    that.setData({
      listFlag: true
    })
    if (e.detail.value === '') {
      that.setData({
        sugData: '',
        listFlag: false

      });
      return;
    }
    
    // 发起suggestion检索请求 
    BMap.suggestion({
      "query": e.detail.value,
      region: '徐州',
      city_limit: true,
      fail: function(data){
        console.log("input error");
       
      },
      success: function(data){
        var len=data.result.length;
        var sugData = '';
        console.log(data.result);
        if(len>1){
          // for (var i = 0; i < data.result.length; i++) {
          //   sugData = sugData + data.result[i].name + '\n';
          // }
         
          that.setData({
            sugDataList:data.result
          })
          console.log(sugDataList);
        }else{
          sugData="没有相关店铺"
        }
        that.setData({
          sugData: sugData
        });
        
        }
     
      
    });
  },
  tiaozhuan:function(e){
    var jumpurl = app.jumpurl.url;
    var p1 = e.currentTarget.dataset.p;
    console.log(p1);
    var that=this;
    that.setData({
      listFlag:false
    })
    wx.request({
      url: jumpurl + 'address/index',
      data: {
        currentTab:2,
        openid:app.userList.openid,
        name:p1.title,
        tel: p1.telephone,
        map_add: p1.title,
        address:p1.address,
        jingwei: p1.longitude + ',' + p1.latitude,
        types:1,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        wx.redirectTo({
         
          url: '/pages/index/index?currentTab=2&shopName=' + p1.address+'&longitude='+p1.longitude+'&latitude='+p1.latitude,
        })
      }
    })
    
//测试用
  // 
},





  showSearchInfo: function (data, i) {
    console.log(data);
    var that = this;
    that.setData({
      placeData: {
        title: '名称：' + data[i].title + '\n',
        address: '地址：' + data[i].address + '\n',
        telephone: '电话：' + data[i].telephone,
        longitude: '电话：' + data[i].longitude,
        latitude: '电话：' + data[i].latitude,
      }
    });
  },
  changeMarkerColor: function (data, i) {
    var that = this;
    var markers = [];
    for (var j = 0; j < data.length; j++) {
      if (j == i) {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_yellow.png";
      } else {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_red.png";
      }
      markers[j](data[j]);
    }
    that.setData({
      markers: markers
    });
  },


  pullBar: function () {
    this.setData({
      pullBar: !this.data.pullBar
    })
  }
  ,
  addToTrolley: function (e) {
    var info = this.data.menu;
    info[this.data.selected].menuContent[e.currentTarget.dataset.index].numb++;
    this.setData({
      cost: this.data.cost+this.data.menu[this.data.selected].menuContent[e.currentTarget.dataset.index].price,
      menu: info,
    })
  },
  removeFromTrolley: function (e) {
    var info = this.data.menu;
    if (info[this.data.selected].menuContent[e.currentTarget.dataset.index].numb!=0){
      info[this.data.selected].menuContent[e.currentTarget.dataset.index].numb--;
      this.setData({
        cost: this.data.cost - this.data.menu[this.data.selected].menuContent[e.currentTarget.dataset.index].price,
        menu: info,
      })
    }
  },
  turnPage: function (e) {
    this.setData({
      currentPage: e.currentTarget.dataset.index
    })
  },
  turnTitle: function (e) {
    if(e.detail.source=="touch"){
      this.setData({
        currentPage: e.detail.current
      })
    }
  },
  turnMenu: function (e) {
    this.setData({
      selected: e.currentTarget.dataset.index
    })
    console.log(e.currentTarget.dataset.index);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad: function (options) {
  //   var that = this;
    // wx.request({
    //   url: "https://www.easy-mock.com/mock/596257bc9adc231f357c4664/restaurant/menu",
    //   method: "GET",
    //   success: function (res) {
    //     console.log(res)
    //     that.setData({
    //       menu: res.data,
    //     })
    //   }
    // });
  // },
  choseAddred:function(e){
    var that = this;
    console.log(e)
    var shopName = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/index/index?shopName=' + shopName + "&currentTab=" +"2",
    })

    this.setData({
      // menu: res.data,
    })
  
  }
 

 

})